﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Hospital_UpdatePayMent : System.Web.UI.Page, HZ.MBSM.BLL.TraDes
{

    public Int32 BulidPayProject;
    public Int64 BulidPayHospitalId;
    public int AccountId_PS;
    public string paymentid;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        //ViewState["company"] = this.txtCompanyName_PS.Text.Trim();
        //if (ViewState["company"] != null)
        //{
        //    this.txtCompanyName_PS.Text = ViewState["company"].ToString();
        //}
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            paymentid = SysFun.IsSQL(Request.QueryString["PaymentId"].ToString());

            BulidPayHospitalId = SysFun.ToLong(_Account.AccountId);

            //************08.13   start

            Db dbone = new Db();
            string str = "select * from Payment  where AccountId_YY = '"
                + _Account.AccountId
                + "'  and Paymentid = '"
                + paymentid
                + "'"
                + " and PaymentState !=1 ";//PaymentState=1表示结款单已经确认，不能修改，其余情况可以修改
            bool isRight = false;
            System.Data.IDataReader drr;
            drr = dbone.DataReader(str);
            while (drr.Read())
            {
                isRight = true; break; 

            }

            drr.Close();
            drr.Dispose();
            dbone.Close();
            if (isRight)
            { 
                //******************** end

                if (!IsPostBack)
                {
                    Db db = new Db();
                    System.Data.IDataReader dr = db.DataReader(@"select paymentMemo,PaymentPrice,paymentname,CompanyName from payment 
left join company
on payment.Accountid_PS=company.Accountid
where paymentid=" + paymentid);
                    while (dr.Read())
                    {
                        // Response.Write("<script language=\"javascript\">   document.all(\"txtCompanyName_PS\").value="+dr["CompanyName"].ToString()+"</script>");

                        txtCompanyName_PS.Text = dr["CompanyName"].ToString();
                        this.txtCompanyName_PS.ReadOnly = true;
                        this.txttotalMoney.Text = dr["PaymentPrice"].ToString();
                        this.txtPaymentMemo.Text = dr["paymentMemo"].ToString();
                        this.txtPaymentName.ReadOnly = true;
                        this.txtPaymentName.Text = dr["PaymentName"].ToString();
                    }
                    dr.Close();
                    System.Data.IDataReader dr1 = db.DataReader("select distinct Receiptid  from BuyPlanGoods where paymentid=" + paymentid);
                    string sumReceiptid = "";
                    while (dr1.Read())
                    {
                        sumReceiptid = sumReceiptid + dr1["Receiptid"].ToString() + ",";
                    }
                    Receiptid.Text = sumReceiptid;
                    dr1.Close();
                    System.Data.IDataReader dr2 = db.DataReader("select Receiptid as 发票号,ProductName as 通用名,MedicineModel 剂型,Outlookc 规格,Pack 包装,Factor 转换系数,QualityLevels 质量层次,CompanyName_PS as 配送企业,Tradeprice as 价格,InflowNum as 数量,Tradeprice*InflowNum as 总金额 from BuyGoodsView_200708 where paymentid=" + paymentid);
                    this.GridView1.DataSource = dr2;
                    this.GridView1.DataBind();
                    dr2.Close();
                }
                 

                if (_Account != null)
                {
                    BulidPayHospitalId = SysFun.ToLong(_Account.AccountId);
                    BulidPayProject = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["ProjectId"]));
                    AccountId_PS = SysFun.ToInt(Request.Form["AccountId_PS"]);

                }
                if (!IsPostBack)
                {
                    string _strHospitalName = _Account.DWName;
                    txtPaymentName.Text = _Account.DWName + System.DateTime.Now.ToShortDateString() + "结算单";
                }
            }
            else
            {
                MsgBox.Aert("非法操作！", "Payment.aspx");
                return;
            }
        }
       

    }
    protected void ButAdd_Click(object sender, EventArgs e)
    {
        Db db = new Db();
        Response.Write("<script>alert('您确定要修改吗,')</script>");
       
        if (Request.Form["txtCompanyName_PS"].ToString()== "")
        {
            Response.Write("<script>alert('请您选择企业名称，即使是同样的企业请您在选择一次')</script>");
        }
        else if ( Request.Form["Receiptid"].ToString() == "")
        {
            Response.Write("<script>alert('请您选择发票号')</script>");
        }
        else if (this.txttotalMoney.Text == "")
        {
            Response.Write("<script>alert('请确认您点击了交易金额吗?')</script>");
        }
        else
        {
            string receiptidstring = this.Receiptid.Text.ToString();
            receiptidstring = receiptidstring.ToString().Substring(0, receiptidstring.Length - 1);
            string updatepay1 = "update payment set Accountid_PS=0,PaymentPrice=0 where paymentid=" + paymentid;
            string delbuy = "update BuyPlanGoods set ispayment=null,paymentid=null where paymentid=" + paymentid;
            string updatepay = "update payment set Accountid_PS='" + Request.Form["AccountId_PS"] + "',PaymentMemo='" + this.txtPaymentMemo.Text + "',PaymentPrice='" + this.txttotalMoney.Text + "' where paymentid='" + paymentid + "'";
            string updateBuyPlanGoods = "update BuyPlanGoods set ispayment=0,paymentid=" + paymentid + " where Accountid_YY='" + BulidPayHospitalId + "' and Accountid_PS='" + Request.Form["Accountid_PS"].ToString() + "' and Receiptid in (" + receiptidstring + ")";
            db.Command(updatepay1);
            db.Command(delbuy);
            db.Command(updatepay);
            db.Command(updateBuyPlanGoods);
            db.Close();
            Response.Write("<script>alert('修改成功')</script>");
            Response.Write("<script>window.close()</script>");



        }
      
      

    }
    protected void btnClose_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.close();</script>");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.HiddenField1.Value = Request.Form["txtCompanyName_PS"].ToString();
        this.txtCompanyName_PS.Text = this.HiddenField1.Value;
        if (Request.Form["Receiptid"].ToString() == "")
        {
            Response.Write("<script>alert('填写发票号')</script>");
        }
        else
        {
          
            //Response.Write(Request.Form["HiddenField1"].ToString());
            //this.HiddenField2.Value = Receiptid.Text.ToString();
            this.HiddenField2.Value = Request.Form["Receiptid"].ToString();
            this.txtCompanyName_PS.Text = this.HiddenField1.Value;
            this.Receiptid.Text = this.HiddenField2.Value;
            string receiptidstring=Request.Form["Receiptid"].ToString();
            receiptidstring=receiptidstring.ToString().Substring(0, receiptidstring.Length - 1);
            Db db = new Db();
            System.Data.IDataReader dr = db.DataReader("select sum(Tradeprice*InflowNum) as zong from BuyGoodsView_200708 where Receiptid in (" + receiptidstring + ") and Accountid_PS='" + Request.Form["Accountid_PS"].ToString() + "'");
            while (dr.Read())
            {
                txttotalMoney .Text= SysFun.ToTrim(dr["zong"]);
            }
            
            dr.Close();
            dr.Dispose();
            
            System.Data.IDataReader dt = db.DataReader("select Receiptid as 发票号,ProductName as 通用名,MedicineModel 剂型,Outlookc 规格,Pack 包装,Factor 转换系数,QualityLevels 质量层次,CompanyName_PS as 配送企业,Tradeprice as 价格,InflowNum as 数量,Tradeprice*InflowNum as 总金额 from BuyGoodsView_200708 where Receiptid in (" + receiptidstring + ") and Accountid_YY='"+BulidPayHospitalId+"' and Accountid_PS='"+Request.Form["AccountId_PS"]+"'");
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
            dt.Close();
            dt.Dispose();
            db.Close();
        }
        
     
    }

    #region TraDes 成员

    public string Name
    {
        get { return "新增结算单"; }
    }

    public string Describe
    {
        get { return ""; }
    }

    public string ParentName
    {
        get { return "结算管理"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
            return _AccountType;
        }
    }

    #endregion
}
